<?php
namespace app\common\model;

use think\Model;
use Carbon\Carbon;
use think\Loader;
use think\Lang;
use think\Request;
use think\Exception;
use app\common\traits\model\TraitModel;

class Caravans extends Model
{
    protected $resultSetType      = 'collection';
    protected $autoWriteTimestamp = 'timestamp';
    protected $createTime         = 'created_at';
    protected $updateTime         = 'updated_at';
    protected $table              = 'caravan';
    protected $type               = [
            'piccaravan'   => 'array',
            'caravancover' => 'array'
    ];
    
    use TraitModel;

    public function user()
    {
        return $this->belongsTo('Users', 'user_id')->setEagerlyType(0);;
    }
    /**
     * 房车
     * @param  [type] $querys [description]
     * @return [type]        [description]
     */
    public function getCaravans($querys = [], $order = 'id desc')
    {
        return $this->getInfoList($querys, $order);
    }

    /**
     * 房车日历
     * @param  [type] $caravan_id [description]
     * @return [type]             [description]
     */
    public function calendar($caravan_id = null)
    {
        $calendar = Loader::model('Calendar');
        Request::instance()->bind('caravan', $caravan_id ? $this->getCaravanInfo(['id' => $caravan_id]) : $this);
        return $calendar;
    }

    public function checkCanPay($caravan_id, $start_date, $end_date = null)
    {
        return $this->calendar()->checkCanPay($caravan_id, $start_date, $end_date);
    }

    /**
     * 获取房车日历状态数组
     * @param  [type] $caravan_id [description]
     * @return [type]             [description]
     */
    public function getCaravanStatus($caravan_id = null)
    {
        return $this->calendar($caravan_id)->getCaravanStatus();
    }

    /**
     * 设置房车分享时间段
     * @param [type] $start_date [description]
     * @param [type] $end_date   [description]
     */
    public function setCaravanShare($start_date, $end_date = null)
    {
        return $this->calendar()->setCaravanShare($start_date, $end_date);
    }

    /**
     * 设置房车自用时间段
     * @param [type] $start_date [description]
     * @param [type] $end_date   [description]
     */
    public function setCaravanHome($start_date, $end_date = null)
    {
        return $this->calendar()->setCaravanHome($start_date, $end_date);
    }

    /**
     * 设置房车托管时间段
     * @param [type]  $start_date [description]
     * @param [type]  $end_date   [description]
     * @param boolean $shares     [description]
     */
    public function setCaravanTrusteeship($start_date, $end_date = null, $shares = true)
    {
        return $this->calendar()->setCaravanTrusteeship($start_date, $end_date, $shares);
    }

    /**
     * 校验所选时间是否分享状态
     * @param  [type] $start_date [description]
     * @param  [type] $end_date   [description]
     * @return [type]             [description]
     */
    public function checkCalendarShared($start_date, $end_date)
    {
        return $this->calendar()->checkCalendarShared($start_date, $end_date);
    }

    /**
     * 校验强制分享时间段是否合法
     * @param  [type] $start_date [description]
     * @param  [type] $end_date   [description]
     * @return [type]             [description]
     */
    public function checkPeakDate($start_date, $end_date)
    {
        return $this->calendar()->checkPeakDate($start_date, $end_date);
    }

    /**
     * 检索房车
     * @param  array  $querys [description]
     * @return [type]         [description]
     */
    public function search(array $querys = [], $page = 0)
    {
        return Loader::model('CarCalendar')->queryCaravans($querys, $page);
    }

    public function camp()
    {
        return $this->belongsTo('\\app\\common\\model\\Camps','services');
    }

    /**
     * 初始化房车日历并设置分享的旺季时间段
     * @param  array  $init_dates 分享的旺季时间段
     * @return [type]             [description]
     */
    public function initCaravanCarlendar(array $init_dates)
    {
        return $this->calendar()->initCaravanCarlendar($init_dates);
    }

    /**
     * 获取房车信息
     * @param  string $querys
     * @return [type]             [description]
     */
    public function getCaravanInfo($querys)
    {
        if ($info = self::get($querys)) {
            return $info;
        }
        throw new Exception(Lang::get("Car information does not exist"), 400001);        
    }

    public function updateCaravanInfo($querys, $caravan_info)
    {
        $this->update($caravan_info, $querys, true);
    }
}